using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExpertSMS.Models;

namespace ExpertSMS.Controllers
{   
    public class UnitsController : Controller
    {
        private ExpertSMSContext context = new ExpertSMSContext();

        //
        // GET: /Units/

        public ViewResult Index()
        {
            return View(context.Units.Include(unit => unit.Product).Include(unit => unit.ParentUnit).Include(unit => unit.ChildUnits).ToList());
        }

        //
        // GET: /Units/Details/5

        public ViewResult Details(int id)
        {
            Unit unit = context.Units.Single(x => x.UnitID == id);
            return View(unit);
        }

        //
        // GET: /Units/Create

        public ActionResult Create(int productID)
        {
            Product product = context.Products.Single(x => x.ProductID == productID);
            ViewBag.Product = product;
            ViewBag.PossibleParentUnits = context.Units.Where(x=>x.ProductID == productID);
            return View();
        } 

        //
        // POST: /Units/Create

        [HttpPost]
        public ActionResult Create(Unit unit)
        {
            if (ModelState.IsValid)
            {
                context.Units.Add(unit);
                context.SaveChanges();
                return RedirectToAction("Details", "Products", new { id = unit.ProductID });  
            }

            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleParentUnits = context.Units;
            return View(unit);
        }
        
        //
        // GET: /Units/Edit/5
 
        public ActionResult Edit(int id)
        {
            Unit unit = context.Units.Single(x => x.UnitID == id);
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleParentUnits = context.Units;
            return View(unit);
        }

        //
        // POST: /Units/Edit/5

        [HttpPost]
        public ActionResult Edit(Unit unit)
        {
            if (ModelState.IsValid)
            {
                context.Entry(unit).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleParentUnits = context.Units;
            return View(unit);
        }

        //
        // GET: /Units/Delete/5
 
        public ActionResult Delete(int id)
        {
            Unit unit = context.Units.Single(x => x.UnitID == id);
            return View(unit);
        }

        //
        // POST: /Units/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Unit unit = context.Units.Single(x => x.UnitID == id);
            context.Units.Remove(unit);
            context.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}